//
//  LZManageReviewsPresenter.m
//  laziz_Merchant
//
//  Created by xbn_iMac on 2016/12/19.
//  Copyright © 2016年 XBN. All rights reserved.
//

#import "LZManageReviewsPresenter.h"
#import "NetworkServiceManager.h"
#import "NSString+Additions.h"
#import "LZUserModel.h"
#import "MJExtension.h"

#import "LAReviewModel.h"
#import "LZReviewsReplyModel.h"
#import "LZManageReviewsPromotionVideoListModel.h"
#import "LZRestaurantCommentInfoModel.h"

extern NSString *merchant;

static NSString *const kPromotionVideoListUrl = @"/promotion/promotionList";    // 评价管理--优惠视频列表接口
static NSString *const kVideoCommentInfoUrl = @"/comment/videoCommentInfo";  // 评论管理--视频评论信息
static NSString *const kListVideoCommentUrl = @"/comment/listVideoComment";  // 评价管理--视频评分详情列表
static NSString *const kMerchantCommentInfoUrl = @"/comment/merchantCommentInfo";  // 评论管理--餐厅评论信息
static NSString *const kListMerchantCommentUrl = @"/comment/listMerchantComment";  // 评价管理--商户评价列表接口
static NSString *const kReplyAddUrl = @"/reply/add";  // 评论列表--添加回复
static NSString *const kReplyDeleteUrl = @"/reply/delete";  // 评论列表--删除评论

@implementation LZManageReviewsPresenter

#pragma mark - ********************************** 优惠视频 **********************************
#pragma mark - 评价管理--优惠视频列表接口
/**
 评价管理--优惠视频列表接口
 
 @param page 页码
 @param limit 每页结果条数
 @param success .
 @param fail .
 */
- (void)promotionVideoListWithPage:(int)page andLimit:(int)limit onSuccess:(PresenterCommonSuccess)success onFail:(PresenterCommonFail)fail {
    
    if ([LZUserModel Instance].userId.length == 0) {
        return;
    }
    
    NSString *urlStr = COMBINATION(merchant,kPromotionVideoListUrl);
    NSDictionary *params = @{@"sellerId":[LZUserModel Instance].sellerId,
                             @"cpage":[NSString stringWithFormat:@"%d",page],
                             @"limit":[NSString stringWithFormat:@"%d",limit]
                             };
    
    [[NetworkServiceManager manager] executePostTask:urlStr parameters:params success:^(NSURLSessionDataTask *task, id resultData) {
        if ([LZBasePresenter isRequestStatusCodeCorrect:resultData fail:fail]) {
            NSMutableArray *arrData = [NSMutableArray array];
            NSArray *arrResult = resultData[@"data"][@"list"];
            for (int i = 0; i<arrResult.count; i++) {
                LZManageReviewsPromotionVideoListModel *model = [LZManageReviewsPromotionVideoListModel mj_objectWithKeyValues:arrResult[i]];
                [arrData addObject:model];
            }
            
            SAFE_BLOCK_INVOKE(success, arrData);
        }
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        SAFE_BLOCK_INVOKE(fail, error.localizedDescription);
    }];
}

#pragma mark - 评论管理--视频评论信息
/**
 评论管理--视频评论信息
 
 @param success .
 @param fail .
 */
- (void)videoCommentInfoWithVideoId:(NSString*)videoId OnSuccess:(PresenterCommonSuccess)success onFail:(PresenterCommonFail)fail {
    NSString *urlStr = COMBINATION(merchant,kVideoCommentInfoUrl);
    
    NSDictionary *params = @{@"videoId":videoId};
    
    [[NetworkServiceManager manager] executePostTask:urlStr parameters:params success:^(NSURLSessionDataTask *task, id resultData) {
        if ([LZBasePresenter isRequestStatusCodeCorrect:resultData fail:fail]) {
            
            LZReviewCountModel *model = [LZReviewCountModel mj_objectWithKeyValues:resultData[@"data"]];
            
            SAFE_BLOCK_INVOKE(success, model);
        }
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        SAFE_BLOCK_INVOKE(fail, error.localizedDescription);
    }];
}

#pragma mark - 评价管理--视频评分详情列表
/**
 评价管理--视频评分详情列表
 
 @param videoId 视频ID
 @param type 类型 0全部 1好评 2差评
 @param page 页码
 @param limit 每页结果条数
 @param success .
 @param fail .
 */
- (void)videoReviewListWithVideoId:(NSString*)videoId andType:(NSString*)type andPage:(int)page andLimit:(int)limit onSuccess:(PresenterCommonSuccess)success onFail:(PresenterCommonFail)fail {
    NSString *urlStr = COMBINATION(merchant,kListVideoCommentUrl);
    NSDictionary *params = @{@"videoId":videoId,
                             @"type":type,
                             @"cpage":[NSString stringWithFormat:@"%d",page],
                             @"limit":[NSString stringWithFormat:@"%d",limit]
                             };
    
    [[NetworkServiceManager manager] executePostTask:urlStr parameters:params success:^(NSURLSessionDataTask *task, id resultData) {
        if ([LZBasePresenter isRequestStatusCodeCorrect:resultData fail:fail]) {
            NSMutableArray *arrData = [NSMutableArray array];
            NSArray *arrResult = resultData[@"data"][@"list"];
            for (int i = 0; i<arrResult.count; i++) {
                LAReviewModel *model = [LAReviewModel mj_objectWithKeyValues:arrResult[i]];
                [arrData addObject:model];
            }
            
            SAFE_BLOCK_INVOKE(success, arrData);
        }
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        SAFE_BLOCK_INVOKE(fail, error.localizedDescription);
    }];
}

#pragma mark - ********************************** 餐厅 **********************************

#pragma mark - 评论管理--餐厅评论信息

/**
 评论管理--餐厅评论信息
 
 @param success .
 @param fail .
 */
- (void)restaurantCommentInfoOnSuccess:(PresenterCommonSuccess)success onFail:(PresenterCommonFail)fail {
    
    if ([LZUserModel Instance].userId.length == 0) {
        return;
    }
    
    NSString *urlStr = COMBINATION(merchant,kMerchantCommentInfoUrl);
    NSDictionary *params = @{@"sellerId":[LZUserModel Instance].sellerId};
    
    [[NetworkServiceManager manager] executePostTask:urlStr parameters:params success:^(NSURLSessionDataTask *task, id resultData) {
        if ([LZBasePresenter isRequestStatusCodeCorrect:resultData fail:fail]) {
            
            LZRestaurantCommentInfoModel *model = [LZRestaurantCommentInfoModel mj_objectWithKeyValues:resultData[@"data"]];
            
            SAFE_BLOCK_INVOKE(success, model);
        }
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        SAFE_BLOCK_INVOKE(fail, error.localizedDescription);
    }];
}

#pragma mark - 评价管理--商户评价列表接口

/**
 评价管理--商户评价列表接口
 
 @param type 类型 0全部 1好评 2差评
 @param page 页码
 @param limit 每页结果条数
 @param success .
 @param fail .
 */
- (void)restaurantReviewListWithType:(NSString*)type andPage:(int)page andLimit:(int)limit onSuccess:(PresenterCommonSuccess)success onFail:(PresenterCommonFail)fail{
    
    if ([LZUserModel Instance].userId.length == 0) {
        return;
    }
    
    NSString *urlStr = COMBINATION(merchant,kListMerchantCommentUrl);
    NSDictionary *params = @{@"sellerId":[LZUserModel Instance].sellerId,
                             @"type":type,
                             @"cpage":[NSString stringWithFormat:@"%d",page],
                             @"limit":[NSString stringWithFormat:@"%d",limit]
                             };
    
    [[NetworkServiceManager manager] executePostTask:urlStr parameters:params success:^(NSURLSessionDataTask *task, id resultData) {
        if ([LZBasePresenter isRequestStatusCodeCorrect:resultData fail:fail]) {
            NSMutableArray *arrData = [NSMutableArray array];
            NSArray *arrResult = resultData[@"data"][@"list"];
            for (int i = 0; i<arrResult.count; i++) {
                LAReviewModel *model = [LAReviewModel mj_objectWithKeyValues:arrResult[i]];
                [arrData addObject:model];
            }

            SAFE_BLOCK_INVOKE(success, arrData);
        }
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        SAFE_BLOCK_INVOKE(fail, error.localizedDescription);
    }];
}

#pragma mark - ********************************** 回复 **********************************

/**
 评论列表--添加回复

 @param messageType 回复类型（1：视频。。2：餐厅。。）
 @param userId 评论的用户id
 @param commentId 评论id
 @param replies 评论内容
 @param success .
 @param fail .
 */
- (void)replyReviewsWithMessageType:(NSString*)messageType andReviewsUserId:(NSString*)userId andCommentId:(NSString*)commentId andReplies:(NSString*)replies onSuccess:(PresenterCommonSuccess)success onFail:(PresenterCommonFail)fail {
    if ([LZUserModel Instance].userId.length == 0) {
        return;
    }
    
    NSString *urlStr = COMBINATION(merchant,kReplyAddUrl);
    NSDictionary *params = @{@"sellerId":[LZUserModel Instance].sellerId,
                             @"messageType":messageType,
                             @"userId":userId,
                             @"commentId":commentId,
                             @"replies":replies,
                             };
    
    [[NetworkServiceManager manager] executePostTask:urlStr parameters:params success:^(NSURLSessionDataTask *task, id resultData) {
        if ([LZBasePresenter isRequestStatusCodeCorrect:resultData fail:fail]) {
            LZReviewsReplyModel *model = [[LZReviewsReplyModel alloc] init];
            model.replyId = resultData[@"data"][@"id"];
            model.replyTime = resultData[@"data"][@"time"];
            model.commentId = commentId;
            model.messageType = messageType;
            model.replies = replies;
            model.sellerId = [LZUserModel Instance].sellerId;
            model.userId = userId;

            SAFE_BLOCK_INVOKE(success, model);
        }
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        SAFE_BLOCK_INVOKE(fail, error.localizedDescription);
    }];
}


/**
 评论列表--删除评论

 @param replyId 回复id
 @param success .
 @param fail .
 */
- (void)deleteReplyReviewsWithReplyId:(NSString*)replyId onSuccess:(PresenterCommonSuccess)success onFail:(PresenterCommonFail)fail {
    
    NSString *urlStr = COMBINATION(merchant,kReplyDeleteUrl);
    NSDictionary *params = @{@"id":replyId,
                             };
    
    [[NetworkServiceManager manager] executePostTask:urlStr parameters:params success:^(NSURLSessionDataTask *task, id resultData) {
        if ([LZBasePresenter isRequestStatusCodeCorrect:resultData fail:fail]) {
            SAFE_BLOCK_INVOKE(success, nil);
        }
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        SAFE_BLOCK_INVOKE(fail, error.localizedDescription);
    }];
}

@end
